var app = getApp();
var hasUserInfo = false;

const fastIcons = [{
    icon: "/images/home-icon-device.png",
    width: 52,
    hegith: 48,
    title: '设备',
    url: '/pages/partner/equipment/equipment',
  }, {
    icon: "/images/card-icon-primary.png",
    width: 52,
    hegith: 48,
    title: '水卡',
    url: '/pages/partner/fictitiousCard/fictitiousCard?is_special=0',
  }, {
    icon: "/images/home-icon-template.png",
    width: 50,
    hegith: 48,
    title: '模板',
    url: '/pages/partner/template/template',
  }, {
    icon: "/images/home-icon-village.png",
    width: 52,
    hegith: 48,
    title: '园区',
    url: '/pages/partner/community/community',
  }, {
    icon: "/images/icon_salesperson.png",
    width: 50,
    hegith: 50,
    title: '销售员',
    url: '/pages/partner/salesman/salesman',
  }, {
    icon: "/images/icon_maintenance.png",
    width: 50,
    hegith: 50,
    title: '维修员',
    url: '/pages/partner/engineering/list/list?from=1',
  }, {
    icon: "/images/icon_franchisee.png",
    width: 50,
    hegith: 50,
    title: '加盟商',
    franchisee: true,
    url: '/pages/partner/franchisee/franchisee',
  }, {
    icon: "/images/icon_achievement.png",
    width: 50,
    hegith: 50,
    title: '业绩',
    url: '/pages/partner/capitalmanage/capitalmanage',
  },
  {
    icon: "/images/icon_more.png",
    width: 50,
    hegith: 50,
    switchTab: true,
    title: '更多服务',
    url: '/pages/partner/home/home',
  }
]
var network_path=app.globalData._network_path;
Page({
  data: {
    network_path,
    fastIcons,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    status: 0,
    statistics: '',
    announcement: [],
    commonList: [],
    userInfo: {},
  },



  onPullDownRefresh() {
    this.onShow();
  },

  onShow: function () {
    hasUserInfo = false;
    this.getPartnerInfo();
    this.getShareRule();
    this.getStatistics();
    this.getAnnouncement();
  },



  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    hasUserInfo = true;
    this.setData({
      userInfo: e.detail.userInfo,
      // hasUserInfo: true
    })
  },

  //登录后获取用户信息
  getPartnerInfo() {
    var that = this;
    app.ajax({
      url: 'Partner/User/getPartnerInfo',
      data: {},
      success: function (res) {
        wx.stopPullDownRefresh();
        if (res.data.code == 200) {
          app.globalData.userInfo = res.data.data;
          hasUserInfo = true;
          //加盟商不显示促销活动和加盟商管理
          that.setData({
            userInfo: res.data.data,
            // hasUserInfo: true
          })
        }
      },
      error: e => {
        wx.stopPullDownRefresh();
      }
    })
  },

  //获取公告列表
  getAnnouncement() {
    app.ajax({
      url: 'Partner/Console/message',
      success: res => {
        if (res.data.code > 0) {
          this.setData({
            announcement: res.data.data.platform_bulletin
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        app.showToast("网络错误");
      }
    })
  },

  //获取统计数据
  getStatistics() {
    app.ajax({
      url: "Partner/Console/homeStatistics",
      success: res => {
        if (res.data.code > 0) {
          console.log(res.data.data);
          this.setData({
            statistics: res.data.data
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        app.showToast("网络错误");
      }
    })
  },
  //公告详情
  navBulletinDetail(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../messageDetails/messageDetails?m_id=' + id,
    })
  },
  //跳转到账户管理
  gotoBalance() {
    wx.navigateTo({
      url: '../account/account',
    })
  },
  //跳转到可提现金额
  gotoWithdraw() {
    wx.navigateTo({
      url: '../putForward/putForward',
    })
  },

  change: function (e) {
    this.setData({
      current: e.detail.current
    })
  },

  navUrl(e) {
    const {
      url,
      switchtab
    } = e.currentTarget.dataset;
    if (switchtab === true) {
      wx.switchTab({
        url
      })
    } else {
      wx.navigateTo({
        url
      })
    }

  },


  //跳转到公告
  navBulletin(e) {
    wx.navigateTo({
      url: '../message/message',
    })
  },

  //公告详情
  navBulletinDetail(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../messageDetails/messageDetails?m_id=' + id,
    })
  },
  //公告列表 1公告 2常见
  systemMsg(e) {
    const {
      type
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/engineering/pages/systemMsg/systemMsg?type=' + type,
    })

  },
  //获取常见问题
  getShareRule() {
    var that = this;
    app.ajax({
      url: "Common/Common/getShareRule",
      data: {
        code: "PHR839WT"
      },
      success: res => {
        if (res.data.code == 1000) {
          this.setData({
            commonList: res.data.data
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //跳转到详情
  navDetail(e) {
    const {
      content,
      title,
      ctime,
      author
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/engineering/pages/systemMsgDetail/systemMsgDetail',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('dispatchContent', {
          content,
          title,
          ctime,
          author
        })
      },

    })
  },

})